package com.black.web.controller.system;

import com.black.common.constant.CacheConstants;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.black.common.core.domain.resp.SysDictDataResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "后台-字典数据Api")
@RestController
@RequestMapping("/admin/dict/data")
public class SysDictDataController extends BaseController {

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "根据字典类型查询缓存中的字典数据")
    @GetMapping("/type/{dictType}")
    public R<List<SysDictDataResp>> findByDictType(@PathVariable(value = "dictType") String dictType) {
        String key = CacheConstants.SYS_DICT_KEY + dictType;
        List<SysDictDataResp> list = (List<SysDictDataResp>) this.redisTemplate.opsForValue().get(key);
        if (list == null) {
            return R.fail("字典数据" + dictType + "的值不存在");
        }
        return R.ok(list);
    }
}
